<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        div{
            width: 100px;
            height: 3000px;
            background: linear-gradient(to bottom,red,#aaaaaa,#dddddd);
        }
        #but{
            position: fixed;
            bottom: 30px;
            right: 30px;
        }
    </style>
</head>
<body>
    <div></div>
    <button id="but" style="display: none;">滚回去</button>
</body>
</html>
<script>
    // window.onscroll = function(){
        var top = document.documentElement.scrollTop
    //     var top1 = document.documentElement.clientHeight
        var butt = document.getElementById('but')
    //     window.onscroll = function(){
    //         var top = document.documentElement.scrollTop
    //         if(top > 400){
    //             butt.style.display = "block";
    //         }else{
    //             butt.style.display = 'none'
    //         }
    //         toTop()
    //     }
    //     function toTop(){
    //         butt.onclick = function(){
    //         ytimes = setInterval(function(){
    //             document.documentElement.scrollTop = top - 200
    //             if(top === 0 || document.documentElement.scrollTop === 0){
    //                 clearInterval(ytimes)
    //             }
    //         },10);
    //         }
    //     }
    // }
    window.onscroll = function(){
        var top = document.documentElement.scrollTop
        if(top > 400){
            butt.style.display = "block";
        }else{
            butt.style.display = 'none'
        }
        butt.onclick = function(){
            // document.documentElement.scrollTop = 0
            window.scrollTo({
                top:0,
                behavior:"smooth"
            })
        }
    }

</script>